package com.oig.sys.base.controller;

import com.oig.common.module.SysPageInfo;
import com.oig.common.module.SysRequest;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.base.dto.UnitDto;
import com.oig.sys.base.service.ITbUnitService;
import com.oig.sys.base.vo.UnitVo;
import com.oig.sys.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Tag(name = "单位管理")
@RestController
@RequestMapping("/unit")
@Slf4j
public class TbUnitController {

    private final ITbUnitService unitService;

    @Autowired
    public TbUnitController(ITbUnitService unitService){
        this.unitService = unitService;
    }


    @PreAuthorize("@pms.hasPermission('/unit/addUnit')")
    @SysLog(value="添加单位",apiUrl="/unit/addUnit")
    @Operation(summary = "添加单位")
    @PostMapping(value = "/addUnit")
    public UnitVo addUnit(@RequestBody @Validated(GroupAdd.class) UnitDto unitDto){
        log.info("add unit:{}",unitDto);
        return this.unitService.addUnit(unitDto);
    }

    @PreAuthorize("@pms.hasPermission('/unit/updateUnit')")
    @SysLog(value="添加单位",apiUrl="/unit/updateUnit")
    @Operation(summary = "添加单位")
    @PostMapping(value = "/updateUnit")
    public UnitVo updateUnit(@RequestBody @Validated(GroupEdit.class) UnitDto unitDto){
        log.info("update unit:{}",unitDto);
        return this.unitService.updateUnit(unitDto);
    }


    @PreAuthorize("@pms.hasPermission('/unit/cancelUnit')")
    @SysLog(value="逻辑删除单位",apiUrl="/unit/cancelUnit")
    @Operation(summary = "逻辑删除单位")
    @PostMapping(value = "/cancelUnit/{code}")
    public SysResponse<String> cancelUnit(@PathVariable("code") String code){
        log.info("cancel unit code={}", code) ;
        this.unitService.cancelUnit(code);
        return SysResponse.ok(code,"逻辑删除单位成功");
    }


    @Operation(summary = "查询备选单位")
    @PostMapping(value = "/findUnit")
    public SysPageInfo<UnitVo> findUnit(@RequestBody SysRequest<UnitDto> unitRequest){
        log.info("find unit:{}",unitRequest);
        return this.unitService.queryList(unitRequest);
    }

}
